<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入姓名"
        v-model="queryForm.name"
        maxlength="15"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入手机号"
        v-model="queryForm.phone"
        maxlength="11"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入身份证"
        v-model="queryForm.idcard"
        maxlength="18"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择审核状态"
      >
        <el-option
          v-for="(val, key) in statusOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        title="搜索"
        >搜索</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="150" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="150" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.USER_SEX[scope.row.sex] }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="200" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.phone !== undefined">
            {{
              scope.row.phone.substr(0, 3) + '*****' + scope.row.phone.slice(8)
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="idcard" label="身份证" width="250" align="center">
        <template slot-scope="scope">
          <template
            v-if="
              scope.row.idcard !== undefined && scope.row.idcard.length == 15
            "
          >
            {{
              scope.row.idcard.substr(0, 4) +
                '******' +
                scope.row.idcard.slice(11)
            }}
          </template>
          <template
            v-if="
              scope.row.idcard !== undefined && scope.row.idcard.length == 18
            "
          >
            {{
              scope.row.idcard.substr(0, 4) +
                '******' +
                scope.row.idcard.slice(14)
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="200" align="center">
        <template slot-scope="scope">
          {{ Dicts.AUTHAPPLY_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        width="300"
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
      >
        <template slot-scope="scope">
          <template v-if="scope.row.status == 0">
            <el-button
              v-permission="'realName:approve'"
              size="mini"
              type="success"
              @click="approveDialog(scope.row)"
              icon="el-icon-check"
              circle
              title="通过"
            ></el-button>
            <el-button
              v-permission="'realName:reject'"
              size="mini"
              type="danger"
              @click="rejectDialog(scope.row.rowId)"
              icon="el-icon-close"
              circle
              title="驳回"
            ></el-button>
          </template>
          <el-button
            v-permission="'realName:detail'"
            size="mini"
            type="info"
            @click="msgDialog(scope.row)"
            icon="el-icon-view"
            circle
            title="详情"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`审批`"
      :visible.sync="dialogApproveFormVisible"
      width="40%"
    >
      <el-form :model="approveForm" label-width="90px" ref="approveForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名:" prop="name">
              {{ approveForm.name }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别:" prop="sex">
              <template slot-scope="scope">
                {{ Dicts.USER_SEX[approveForm.sex] }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年龄:" prop="age">
              <template slot-scope="scope">
                {{ approveForm.age }}
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号:" prop="idcard">
              <template slot-scope="scope">
                {{ approveForm.idcard }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话:" prop="phone">
              {{ approveForm.phone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="身份证正面:" prop="idcardFront">
              <template slot-scope="scope">
                <img :src="$oss(approveForm.idcardFront)" width="100%" />
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <img src="" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogApproveFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="approve(approveForm.rowId)"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog
      :title="`驳回原因`"
      :visible.sync="dialogRejectFormVisible"
      width="40%"
    >
      <el-form
        :model="rejectForm"
        label-width="90px"
        :rules="rejectFormRules"
        ref="rejectForm"
      >
        <el-input type="textarea" v-model="rejectForm.remark"></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogRejectFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="rejectAuthApply">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="`身份信息`"
      :visible.sync="dialogMsgFormVisible"
      width="40%"
    >
      <el-form :model="approveForm" label-width="90px" ref="approveForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名:" prop="name">
              {{ approveForm.name }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别:" prop="sex">
              <template slot-scope="scope">
                {{ Dicts.USER_SEX[approveForm.sex] }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年龄:" prop="age">
              <template slot-scope="scope">
                {{ approveForm.age }}
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号:" prop="idcard">
              <template slot-scope="scope">
                {{ approveForm.idcard }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话:" prop="phone">
              {{ approveForm.phone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="身份证正面:" prop="age">
              <template slot-scope="scope">
                <img :src="$oss(approveForm.idcardFront)" width="100%" />
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <img src="" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogMsgFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryAuthApply,
  approveAuthApply,
  rejectAuthApply
} from '@/api/auditing'
export default {
  name: 'RealName',
  data() {
    return {
      dialogRejectFormVisible: false,
      dialogApproveFormVisible: false,
      dialogMsgFormVisible: false,
      loading: false,
      rejectFormRules: {
        remark: [{ required: true, message: '请输入驳回原因', trigger: 'blur' }]
      },
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        importance: undefined,
        title: undefined
      },
      rejectForm: {
        rowId: '',
        remark: ''
      },
      approveForm: {
        rowId: '',
        name: '',
        sex: '',
        phone: '',
        idcard: '',
        idcardFront: '',
        idcardBack: ''
      },
      statusOptions: Dicts.AUTHAPPLY_STATUS,
      sexOptions: Dicts.USER_SEX,
      records: []
    }
  },
  created() {
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryAuthApply(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    approveDialog({ rowId, name, sex, age, idcardFront, idcard, phone }) {
      this.openApproveDialog()
      this.approveForm = {
        rowId: rowId,
        name: name,
        sex: sex,
        age: age,
        idcardFront: idcardFront,
        idcard: idcard,
        phone: phone
      }
      // selectAuthApplyById(rowId).then(({ data: { data, msg }}) => {
      //   this.approveForm = data
      // })
    },
    openApproveDialog() {
      this.approveForm = {}
      this.dialogApproveFormVisible = true
      this.$refs.approveForm && this.$refs.approveForm.resetFields()
    },
    msgDialog({ name, sex, age, idcardFront, idcard, phone }) {
      this.openMsgDialog()
      this.approveForm = {
        name: name,
        sex: sex,
        age: age,
        idcardFront: idcardFront,
        idcard: idcard,
        phone: phone
      }
      // selectAuthApplyById(rowId).then(({ data: { data, msg }}) => {
      //   this.openMsgDialog()
      //   this.approveForm = data
      // })
    },
    openMsgDialog() {
      this.approveForm = {}
      this.dialogMsgFormVisible = true
      this.$refs.approveForm && this.$refs.approveForm.resetFields()
    },
    approve(rowId) {
      this.$confirm('确认通过此申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        approveAuthApply(rowId).then(({ data: { data, msg } }) => {
          this.dialogApproveFormVisible = false
          this.doQuery()
          this.$message({
            showClose: true,
            message: '操作成功!',
            type: 'success'
          })
        })
      })
    },
    rejectDialog(rowId) {
      this.openRejectDialog()
      this.rejectForm.rowId = rowId
    },
    openRejectDialog() {
      this.rejectForm = {}
      this.dialogRejectFormVisible = true
      this.$refs.rejectForm && this.$refs.rejectForm.resetFields()
    },
    rejectAuthApply() {
      this.$confirm('确认驳回此申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        rejectAuthApply(this.rejectForm).then(({ data: { data, msg } }) => {
          this.dialogRejectFormVisible = false
          this.doQuery()
          this.$message({
            showClose: true,
            message: '操作成功!',
            type: 'success'
          })
        })
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
